<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.taskName"
        type="text"
        placeholder="请输入任务名称"
        clearable
      />
      <el-select
        v-model="paramsForm.taskState"
        placeholder="状态"
        clearable
      >
        <el-input
          v-model="paramsForm.tableNameList"
          type="text"
          placeholder="请输入表名列表"
          clearable
        />
        <el-option
          v-for="item in stateOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-date-picker
        v-model="paramsForm.hotStandbyTime"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00','23:59:59']"
        value-format="yyyy-MM-dd HH:mm:ss"
        clearable
      />

      <div style="display: flex">
        <el-button
          type="success"
          icon="el-icon-search"
          @click="getData"
        >
          搜索
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-refresh-left"
          @click="resetForm"
        >
          重置
        </el-button>
      </div>
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      border
    >
      <el-table-column
        label="任务名称"
        prop="taskName"
        sortable
        fixed
        min-width="100px"
      />
      <el-table-column
        label="库名"
        prop="databaseName"
        sortable
      />
      <el-table-column
        label="表名列表"
        prop="tableNameList"
        sortable
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="()=>{
              selectedTableNameList = scope.row.tableNameList
              dialogVisible = true
            }"
          >
            查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="表数量"
        prop="tableHdfsNumber"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="源总数"
        prop="sourceCountNumber"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="目标总数"
        prop="targetCountNumber"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        label="状态"
        prop="taskState"
        sortable
        min-width="80px"
      >
        <template slot-scope="scope">
          <el-tag
            :type="getStateColor(scope.row.taskState)"
          >
            {{ scope.row.taskState }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="金桥开始时间"
        prop="sourceStandbyTimeSta"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.sourceStandbyTimeSta) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="金桥结束时间"
        prop="sourceStandbyTimeEnd"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.sourceStandbyTimeEnd) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="万国开始时间"
        prop="targetStandbyTimeSta"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.targetStandbyTimeSta) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="万国结束时间"
        prop="targetStandbyTimeEnd"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.targetStandbyTimeEnd) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="() => {
              selectedTaskId = scope.row.id
              fullAuditDialogVisible = true
            }
            "
          >
            稽核明细
          </el-button>
          <el-button
            type="text"
            @click="() => {
              selectedTaskId = scope.row.id
              comparisonDialogVisible = true
            }
            "
          >
            比对明细
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="结束时间"
        prop="lastUpdateTime"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ (scope.row.taskState && scope.row.taskState === '进行中') ? '' : scope.row.lastUpdateTime }}</span>
        </template>
      </el-table-column> -->

    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <TableNameList
      :dialog-visible="dialogVisible"
      :table-name-list="selectedTableNameList"
      @close="()=>{
        dialogVisible = false
      }"
    />

    <ComparisonDialog
      :dialog-visible="comparisonDialogVisible"
      :task-id="selectedTaskId"
      @close="()=>{
        comparisonDialogVisible = false
      }"
    />

    <FullAuditDialog
      :dialog-visible="fullAuditDialogVisible"
      :task-id="selectedTaskId"
      @close="()=>{
        fullAuditDialogVisible = false
      }"
    />
  </div>
</template>

<script>
import logAnalysisApi from '@/api/logAnalysis/index'
import dateUtil from '@/utils/date'
import TableNameList from './components/TableNameListDialog.vue'
import ComparisonDialog from './components/ComparisonDialog.vue'
import FullAuditDialog from './components/FullAuditDialog.vue'

export default {
  components: {TableNameList, ComparisonDialog, FullAuditDialog},
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      paramsForm: {
        taskName: '',
        tableNameList: '',
        current: 1,
        size: 20,
        taskState: '',
        hotStandbyTime: [
          dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0))),
          dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1))
        ]
      },
      dialogVisible: false,
      comparisonDialogVisible: false,
      fullAuditDialogVisible: false,
      selectedTaskId: '',
      selectedTableNameList: '',
      stateOption: [
        {
          label: '失败',
          value: '失败'
        },
        {
          label: '已完成',
          value: '已完成'
        },
        {
          label: '进行中',
          value: '进行中'
        }
      ]
    }
  },
  created() {
    this.getData()
  },

  methods: {
    convertDate(timeStamp) {
      if (!timeStamp) { return '' }
      return dateUtil.getDateStr(new Date(timeStamp))
    },
    getStateColor(state) {
      if (state === '已完成') {
        return 'success'
      } else if (state === '失败') {
        return 'danger'
      } else if (state === '进行中') {
        return ''
      } else {
        return 'warning'
      }
    },
    getData() {
      this.isLoading = true
      const param = {
        taskName: this.paramsForm.taskName,
        taskState: this.paramsForm.taskState,
        tableNameList: this.paramsForm.tableNameList,
        current: this.paramsForm.current,
        size: this.paramsForm.size,
        staDate: this.paramsForm.hotStandbyTime ? this.paramsForm.hotStandbyTime[0] : '',
        endDate: this.paramsForm.hotStandbyTime ? this.paramsForm.hotStandbyTime[1] : ''
      }
      logAnalysisApi.getSyncFullAuditTask(param)
        .then((res) => {
          this.data = res.records
          this.total = res.total
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    resetForm() {
      this.paramsForm = {
        current: 1,
        size: 10,
        hotStandbyTime: [
          dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0))),
          dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1))
        ]
      }
      this.getData()
    },
    // 分页
    handleSizeChange(val) {
      this.paramsForm.current = 1
      this.paramsForm.size = val
      this.getData()
    },
    // 分页
    handleCurrentChange(val) {
      this.paramsForm.current = val
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
